home *** CD-ROM | disk | FTP | other *** search
/ ASME's Mechanical Engine…ing Toolkit 1997 December / ASME's Mechanical Engineering Toolkit 1997 December.iso / c_lang / pccstdio.lzh / SRC.LZH / CSETUP.A < prev    next >
Encoding:
Text File  |  1985-05-17  |  2.6 KB  |  145 lines

  1. ;    csetup.a - initialize C program.
  2. ;    (C) Copyright 1984, 1985 Gregory R. Mansfield - All Rights Reserved
  3. ;    G. R. Mansfield.  84/06/01.
  4. ;    Ver 2.0-5517.
  5.  
  6.  
  7.     include    "include/msdos.ah"
  8.     include    "include/ascii.ah"
  9.  
  10. ;    MS-DOS offsets relative to DS.
  11.  
  12. CML    equ    00081h        ; command line
  13. EAB    equ    00002h        ; end of allocated block
  14.  
  15. ;    Linker data offsets relative to SS.
  16.  
  17. UDA    equ    00000h        ; unitialized data address
  18. UDL    equ    00002h        ; unitialized data length
  19.  
  20. ;    csetup values.
  21.  
  22. APTL    equ    512*2        ; length of apt    
  23.  
  24.  
  25.     dseg
  26.     public    errno_
  27.     public    _mcv
  28.     public    _pfb
  29.  
  30. errno_    dw    0
  31. _mcv    dw    0,0        ; memory control vector
  32. _pfb    dw    0        ; program prefix block
  33.  
  34. mmea    db    'not enough memory',CV_LF,'$'
  35.  
  36.     cseg
  37.     public    _csetup
  38.     public    exit_
  39.     public    main_
  40.     public    _setargs_
  41.  
  42.  
  43. ;    mme - memory error.
  44.  
  45. mme:    mov    dx,offset mmea    ; display memory error
  46.     mov    ax,ss
  47.     mov    ds,ax
  48.     mov    ah,FR_DST
  49.     int    I_FCN
  50.     jmp    exit_
  51.  
  52.  
  53. ;    _csetup - initialize C program execution.
  54.  
  55. _csetup: 
  56.     cld
  57.     mov    ax,ds        ; save program prefix block segment
  58.     mov    ss:_pfb,ax
  59.     mov    ax,[EAB]    ; check data segment length
  60.     mov    bx,ss
  61.     sub    ax,bx
  62.     jbe    mme        ; if room for data segment
  63.     dec    ax        ; ds = max(space, 64K)
  64.     mov    bx,00FFFh
  65.     cmp    ax,bx
  66.     jb    csu1
  67.     mov    ax,bx
  68. csu1:    mov    cl,4        ; maximum offset
  69.     shl    ax,cl
  70.     mov    bx,7Fh        ; check stack setting
  71.     mov    di,ss:[UDA]    ; start of uninitialized data
  72.     add    bx,di
  73.     add    bx,ss:[UDL]    ; length of uninitialized data
  74.     cmp    sp,bx
  75.     jnz    csu4        ; if stack set by linker
  76.     mov    sp,ax        ; stack at maximum offset
  77. csu4:    cmp    bx,ax
  78.     jae    mme        ; if no room
  79.  
  80. ;    copy argument string.
  81.  
  82.     push    ss
  83.     pop    es
  84.     mov    bx,di
  85.     mov    si,CML-1    ; copy argument string
  86.     lodsb
  87.     cbw
  88.     mov    cx,ax
  89.     rep movsb
  90.     xchg    al,ch
  91.     stosb
  92.     push    es
  93.     pop    ds
  94.     push    di        ; save start of apt
  95.     mov    bp,sp
  96.     push    bx        ; s
  97.     push    di        ; apt
  98.     add    di,APTL        ; ast
  99.     push    di
  100.     call    _setargs_    ; setargs(ast, apt, s);
  101.     cld
  102.     mov    sp,bp
  103.     pop    bx        ; apt
  104.     mov    si,[bx]        ; ast
  105.     add    bx,ax
  106.     add    bx,ax
  107.     mov    cx,[bx]        ; end of ast = apt[aptl]
  108.     sub    cx,si        ; astl
  109.     sub    sp,cx        ; copy ast to stack
  110.     and    sp,0FFFEh
  111.     mov    di,sp
  112.     rep movsb
  113.     mov    cx,ax        ; argv = apt with adjustment
  114.     sub    di,si
  115. arg1:    dec    bx
  116.     dec    bx
  117.     mov    dx,[bx]
  118.     add    dx,di
  119.     push    dx
  120.     loop    arg1
  121.     mov    dx,sp        ; *argv
  122.     push    dx
  123.     push    ax        ; argc
  124.     
  125.     mov    di,[UDA]    ; start of uninitialized data
  126.     mov    cx,[UDL]    ; length of uninitialized data
  127.     xor    ax,ax
  128.     rep    stosb
  129.     add    di,2        ; set low memory address
  130.     and    di,-2
  131.     mov    _mcv,di
  132.     mov    _mcv+2,di
  133.     call    main_        ; main(argc, argv)
  134.     mov    al,0        ; exit code = 0
  135.     push    ax
  136.     push    ax
  137.  
  138. ;    exit(code)
  139. ;    int code;
  140.  
  141. exit_:    pop    ax        ; skip return address
  142.     pop    ax        ; return code
  143. ext1:    mov    ah,FR_TRP
  144.     int    I_FCN
  145.